﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using HZ.Supervise.Model.Enum;
using HZ.Supervise.Model.View;


namespace HZ.Supervise.Web.Secondphase.NewReport
{
    public partial class CompanyPSL_PS : BasePage
    {
        public CompanyPSL_PS()
        {
            this.ModuleNo = Module.配送企业汇总统计.ToString();
            this.PageOperateType = OperateType.医疗机构汇总统计.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.AreaControl1.InitAreaID = CurrentAccount.GetUser().Org.Area.AreaID;
                BindData();
                BindDatahuizong();
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_chaxun_Click(object sender, EventArgs e)
        {
            AspNetPager_CompanyPSL_hz_PS.CurrentPageIndex = 1;
            AspNetPager_CompanyPSL_PS.CurrentPageIndex = 1;
        }

        /// <summary>
        /// 绑定数据
        /// modify  by张雄  2011.5.4
        /// </summary>
        public void BindData()
        {
            SuperviseQuery vs = new SuperviseQuery();
            vs = Queryby();
            vs.Flag = "0";
            int t = 0;
            HZ.Supervise.IBLL.IExecuteContext iEC = HZ.Supervise.BLLFactory.ExecuteContext();
            System.Data.DataTable dt = iEC.ExecutePagePSLOne(vs, this.AspNetPager_CompanyPSL_PS.CurrentPageIndex, AspNetPager_CompanyPSL_PS.PageSize, out t);

            AspNetPager_CompanyPSL_PS.RecordCount = t;

            this.gvwxianquInfo.PageSize = AspNetPager_CompanyPSL_PS.PageSize;
            this.gvwxianquInfo.DataSource = dt;

            this.gvwxianquInfo.DataBind();
            
        }

        /// <summary>
        /// 分页    县区
        /// by张雄
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void CompanyPSL_PS_PageChanged(object sender, EventArgs e)
        {
            BindData();
            this.divTable.Visible = true;
            this.divTable.Attributes.Add("style", "display:block;");
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>document.getElementById(\"btnShowImg\").value = \"隐藏县区数据\";</script>");
        }

        /// <summary>
        /// 绑定数据    汇总
        /// by张雄  2011.5.26
        /// </summary>
        public void BindDatahuizong()
        {

            SuperviseQuery vs = new SuperviseQuery();
            vs = Queryby();
            vs.Flag = "1";
            int t = 0;
            HZ.Supervise.IBLL.IExecuteContext iEC = HZ.Supervise.BLLFactory.ExecuteContext();
            System.Data.DataTable dt = iEC.ExecutePagePSLOne(vs, this.AspNetPager_CompanyPSL_hz_PS.CurrentPageIndex, AspNetPager_CompanyPSL_hz_PS.PageSize, out t);

            AspNetPager_CompanyPSL_hz_PS.RecordCount = t;
        

            this.gvwPSLInfo.PageSize = AspNetPager_CompanyPSL_hz_PS.PageSize;

            

            this.gvwPSLInfo.DataSource = dt;         

            this.gvwPSLInfo.DataBind();
        }


        /// <summary>
        /// 分页    汇总
        /// by张雄  2011.5.26
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void AspNetPager_CompanyPSL_hz_PS_PageChanged(object sender, EventArgs e)
        {
            BindDatahuizong();
            this.divTable.Visible = true;
            this.divTable.Attributes.Add("style", "display:block;");
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>document.getElementById(\"btnShowImg\").value = \"隐藏县区数据\";</script>");
        }


        /// <summary>
        /// 导出
        /// modify  by  张雄    2011.5.5
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            SuperviseQuery vs = new SuperviseQuery();
            vs = Queryby();
            vs.Flag = "0";
            HZ.Supervise.IBLL.IExecuteContext iEC = HZ.Supervise.BLLFactory.ExecuteContext();
            int t = 0;
            System.Data.DataTable dt = iEC.ExecutePagePSLOne(vs, 1, 0, out t);
          
            ExportFile.Instance.ExportData(dt, gvwxianquInfo, this.Page);
        }

        /// <summary>
        /// 导出-汇总
        /// modify  by  张雄    
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExcel1_Click(object sender, EventArgs e)
        {
            //获得排序的值

            SuperviseQuery vs = new SuperviseQuery();
            vs = Queryby();
            vs.Flag = "1";            HZ.Supervise.IBLL.IExecuteContext iEC = HZ.Supervise.BLLFactory.ExecuteContext();
            int t = 0;
            System.Data.DataTable dt = iEC.ExecutePagePSLOne(vs, 1, 0, out t);
            ExportFile.Instance.ExportData(dt, gvwPSLInfo, this.Page);
        }


        /// <summary>
        /// 查询条件
        /// </summary>
        /// <returns></returns>
        private SuperviseQuery Queryby()
        {
            SuperviseQuery vs = new SuperviseQuery();

            string sortExpression = this.gvwPSLInfo.Attributes["SortExpression"];
            string sortDirection = this.gvwPSLInfo.Attributes["SortDirection"];
            if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
            {
                vs.SortFields = sortExpression + " " + sortDirection;
               
            }
            vs.CompanyName_PS = txtCompanyName_PS.Text.Trim();
            if (!string.IsNullOrEmpty(txtStratTime.Text))
            {
                vs.PlanTimeFrom = DateTime.Parse(txtStratTime.Text.Trim());

            }
            if (!string.IsNullOrEmpty(txtEndTime.Text))
            {
                vs.PlanTimeTo = Convert.ToDateTime(txtEndTime.Text.Trim());

            }
            vs.AreaID = AreaControl1.SelectedAreaID;
            string Level = string.Empty;
            for (int i = 0; i < cblboxlist.Items.Count; i++)
            {
                if (cblboxlist.Items[i].Selected == true)
                {
                    Level += "'" + cblboxlist.Items[i].Value + "',";
                }
            }


            if (!string.IsNullOrEmpty(Level))
            {
                vs.Levels = Level.Substring(0, Level.Length - 1);
            }


            string isCountry = string.Empty;
            for (int i = 0; i < chkIsCountry.Items.Count; i++)
            {
                if (chkIsCountry.Items[i].Selected == true)
                {
                    isCountry += "'" + chkIsCountry.Items[i].Value + "',";
                }
            }


            if (!string.IsNullOrEmpty(isCountry))
            {
                vs.IsCountry = isCountry.Substring(0, isCountry.Length - 1);
            }
            return vs;
        }


        public string SetCount(object obj, object obj1)
        {
            try
            {
                long count = Convert.ToInt64(obj);
                long count1 = Convert.ToInt64(obj1);
                return Math.Round((count1 * 1.0000 / count * 100), 2).ToString() + "%";
            }
            catch (Exception)
            {

                return "100%";
            }
        }
        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            // 从事件参数获取排序数据列
            string sortExpression = e.SortExpression.ToString();

            // 假定为排序方向为“顺序”
            string sortDirection = "ASC";

            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (sortExpression == this.gvwPSLInfo.Attributes["SortExpression"])
            {
                //获得下一次的排序状态
                sortDirection = (this.gvwPSLInfo.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
            }

            // 重新设定GridView排序数据列及排序方向
            this.gvwPSLInfo.Attributes["SortExpression"] = sortExpression;
            this.gvwPSLInfo.Attributes["SortDirection"] = sortDirection;

            BindDatahuizong();
        }

    }
}
